package photobox.manage

import grails.transaction.Transactional
import photobox.account.Member
import photobox.account.User
import photobox.account.UserBrowse
import photobox.photo.Photo
import photobox.photo.PhotoComment
import photobox.photo.PhotoFrame
import photobox.photo.PhotoSize
import photobox.photo.Category
import photobox.utils.HQLUtils

import java.text.SimpleDateFormat

@Transactional
class PhotoService {
    def createPhoto(params,member) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd")
        def photo = new Photo()
        photo.isDeal = Photo.ISDEAL_YES
        photo.laudNum =0
        photo.saleNum =0
        photo.shareNum =0
        photo.originImagePath =params.origin_image_path
        photo.smallImagePath =params.small_image_path
        photo.middleImagePath =params.middle_image_path
        photo.bigImagePath =params.big_image_path
        photo.showShape = Integer.parseInt(params.show_shape)
        photo.picTime = df.parse(params.picTime)
        photo.description = params.description
        photo.keywords = params.keywords
        photo.device = params.device
        photo.name = params.name
        photo.user = member
        photo.display = 1

        photo.price =Float.parseFloat(params.price)
        if(params.photoSize?.size()>0){
            params.photoSize.each{
                def photoSizeId = it
                def photoSize = PhotoSize.get(photoSizeId)
                photo.addToPhotoSize(photoSize)
            }
        }
//        for(def i =0 ;i<params. category?.size();i++){
//            def it = params. category[i]
//        }
        def categorys = []
        if(params.categoryTheme?.size()>0 || params.categoryStyle?.size()>0){
            if (params.categoryStyle instanceof String) {
                params.categoryStyle = [params.categoryStyle]
            }
            if (params.categoryTheme instanceof String) {
                params.categoryTheme = [params.categoryTheme]
            }
            params.categoryTheme.each {
                categorys.push(it)
            }
            params.categoryStyle.each {
                categorys.push(it)
            }

            categorys.each{
                def  categoryId = it
                def  category =  Category.get(categoryId)
                if (category) {
                    photo.addToCategories(category)
                }
            }
        }

        def category = Category.get(categorys)
        photo.addToCategories(category)

        if(!photo.save()){
           println(photo.errors)
        }
    }
    def updateLaudNum(member, photo){
        def userBrowse = UserBrowse.findByUserAndPhoto(member, photo)
        userBrowse.laud = 1
        userBrowse.photo.laudNum += 1
        if (!userBrowse.save()) {
            println(userBrowse.errors)
        }
        return userBrowse
    }
    def createComment(params,user) {
        def photoComment = new PhotoComment()
        photoComment.comment = params.comment

        def photo = Photo.get(params.id)
        photoComment.photo  = photo


        photoComment.user = user
        if(! photoComment.save()){
            println(photoComment.errors)
        }

        return photoComment
    }
    def listPhoto(searchParam, pageParam, sortParam) {
        def hql = ""
        hql += " from Photo A where 1=1 "
        def params = [:]
        if (searchParam.id) {
            hql += " and " + HQLUtils.whereCondition("A.id", searchParam.id.opt, "id")
            params.id = searchParam.id.data
        }
        if (searchParam.name) {
            hql += " and " + HQLUtils.whereCondition("A.name", searchParam.name.opt, "name")
            params.name = searchParam.name.data
        }
        if (searchParam.device) {
            hql += " and " + HQLUtils.whereCondition("A.device", searchParam.device.opt, "device")
            params.device = searchParam.device.data
        }
        if (searchParam.price) {
            hql += " and " + HQLUtils.whereCondition("A.price", searchParam.price.opt, "price")
            params.price = searchParam.price.data
        }
        if (searchParam.picTime) {
            hql += " and " + HQLUtils.whereCondition("A.picTime", searchParam.picTime.opt, "picTime")
            params.picTime = searchParam.picTime.data
        }
        if (searchParam.dateCreated) {
            hql += " and " + HQLUtils.whereCondition("A.dateCreated", searchParam.dateCreated.opt, "dateCreated")
            params.dateCreated = searchParam.dateCreated.data
        }
        if (searchParam.laudNum) {
            hql += " and " + HQLUtils.whereCondition("A.laudNum", searchParam.laudNum.opt, "laudNum")
            params.laudNum = searchParam.laudNum.data
        }
        if (searchParam.shareNum) {
            hql += " and " + HQLUtils.whereCondition("A.shareNum", searchParam.shareNum.opt, "shareNum")
            params.shareNum = searchParam.shareNum.data
        }
        if (searchParam.saleNum) {
            hql += " and " + HQLUtils.whereCondition("A.saleNum", searchParam.saleNum.opt, "saleNum")
            params.saleNum = searchParam.saleNum.data
        }
        if (searchParam.isDeal) {
            hql += " and " + HQLUtils.whereCondition("A.isDeal", searchParam.isDeal.opt, "isDeal")
            params.isDeal = searchParam.isDeal.data
        }
        if (searchParam.display) {
            hql += " and " + HQLUtils.whereCondition("A.display", searchParam.isDeal.opt, "display")
            params.display = searchParam.display.data
        }
        if (searchParam.isRecommend) {
            hql += " and " + HQLUtils.whereCondition("A.isRecommend", searchParam.isRecommend.opt, "isRecommend")
            params.isRecommend = searchParam.isRecommend.data
        }
        def sortHql = ""
        if (sortParam.sort) {
            sortHql = " order by A.${sortParam.sort} ${sortParam.order} "
        }
        def results = Photo.executeQuery("select distinct A " + hql + sortHql, params,
                [max: pageParam.pageSize, offset: pageParam.offset])
        def count = Photo.executeQuery("select count(distinct A.id) " + hql, params)
        return [rs:results, total:count[0]]
    }
    def createUserBrowse(member, photo) {
        def userBrowse = new UserBrowse()
        userBrowse.laud = 0
        userBrowse.photo = photo
        userBrowse.user = member
        if (!userBrowse.save()) {
            println(userBrowse.errors)
        }
        return userBrowse
    }
    def updateUserBrowse(member, photo) {
        def userBrowse = UserBrowse.findByUserAndPhoto(member, photo)
        userBrowse.lastUpdated = null
        if (!userBrowse.save()) {
            println(userBrowse.errors)
        }
        return userBrowse
    }
}


